﻿using System.IO.Compression;

namespace Mohe.Scada.Util.Helper
{
    public static class CompressHelper
    {
        /// <summary>
        /// Zlib压缩
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public static byte[] ZlibCompress(this byte[] data)
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
                {
                    gzipStream.Write(data, 0, data.Length);
                }
                return memoryStream.ToArray();
            }
        }
        /// <summary>
        /// Zlib解压
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static byte[] ZlibUncompress(this byte[] data)
        {
            using (var memoryStream = new MemoryStream(data))
            {
                using (var outputStream = new MemoryStream())
                {
                    using (var decompressStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                    {
                        decompressStream.CopyTo(outputStream);
                    }
                    return outputStream.ToArray();
                }
            }
        }
    }
}
